﻿using CSharpTestProject.Utils;
using System;
using System.Data;
using System.Windows.Forms;

namespace CSharpTestProject.AdminFormFolder
{
    public partial class ModifyStudentForm : Form
    {
        DataTable DataTable;
        public _Sql sql;
        int index;
        public ModifyStudentForm()
        {
            index = 0;
            InitializeComponent();
            button5.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id;
            string name;
            string bj;
            string kcm;
            string s = "SELECT * FROM students WHERE";
            if (textBox1.Text != "")
            {
                id = textBox1.Text;
                s += $" xh='{id}' AND";
            }
            if (textBox2.Text != "")
            {
                name = textBox2.Text;
                s += $" name='{name}' AND";
            }
            if (textBox3.Text != "")
            {
                bj = textBox3.Text;
                s += $" bj='{bj}' AND";
            }
            if (textBox4.Text != "")
            {
                kcm = textBox4.Text;
                s += $" kcm='{kcm}' AND";
            }
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            s = s.Substring(0, s.Length - 3);
            DataSet dataSet = sql.SqlFind(s);
            DataTable = dataSet.Tables[0];
            textBox1.Text = DataTable.Rows[index][1].ToString();
            textBox2.Text = DataTable.Rows[index][2].ToString();
            textBox3.Text = DataTable.Rows[index][3].ToString();
            textBox4.Text = DataTable.Rows[index++][4].ToString();
            button5.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (index >= DataTable.Rows.Count)
            {
                MessageBox.Show("已经显示全部学生，将回到第一个学生");
                index = 0;
            }
            textBox1.Text = DataTable.Rows[index][1].ToString();
            textBox2.Text = DataTable.Rows[index][2].ToString();
            textBox3.Text = DataTable.Rows[index][3].ToString();
            textBox4.Text = DataTable.Rows[index++][4].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            button5.Enabled=false;
            index = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
            textBox3.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string s = $"UPDATE students SET name='{textBox2.Text}',bj='{textBox3.Text}' WHERE xh='{textBox1.Text}' AND kcm='{textBox4.Text}'";
            bool isOk = sql.ManageData(s);
            if (isOk)
            {
                MessageBox.Show("修改成功！");
            } else
            {
                MessageBox.Show("修改失败！");
            }
        }
    }
}
